﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class F110 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string v_str_ID = Request.QueryString["Id"];
            int v_int_ID = 0;
            int.TryParse(v_str_ID, out v_int_ID);
            if (v_int_ID != 0)
            {
                this.m_cmd_tao_moi.Enabled = false;
                this.m_cmd_cap_nhat.Enabled = true;
            }
            else
            {
                this.m_cmd_tao_moi.Enabled = true;
                this.m_cmd_cap_nhat.Enabled = false;
            }

            BindData();
        }
    }




    private void BindData()
    {
        try
        {
            BindLabel();
            BindUpdate();
            List<DM_SAN_PHAM> lst = Services.GetDsDM_SAN_PHAM();
            if (lst != null)
            {
                gvDsMonHoc.DataSource = lst;
                gvDsMonHoc.DataBind();
                // Set event for Delete button
                foreach (GridViewRow row in gvDsMonHoc.Rows)
                {
                    ImageButton imageButton = (ImageButton)row.FindControl("imgBtDelete");
                    imageButton.Attributes.Add("OnClick", "javascript:return confirm('Bạn có chắc chắn muốn xóa sản phẩm này?')");
                }
            }
            else
            {

            }

        }
        catch (Exception)
        {

        }

    }

    private void BindUpdate()
    {
        string v_str_ID = Request.QueryString["Id"];
        int v_int_ID = 0;
        int.TryParse(v_str_ID, out v_int_ID);
        if (v_int_ID != 0)
        {
            DM_SAN_PHAM v_obj_DM_SAN_PHAM = Services.GetDM_SAN_PHAM(v_int_ID);
            txtMaSanPham.Text = v_obj_DM_SAN_PHAM.MA_SAN_PHAM;
            txtTenSanPham.Text = v_obj_DM_SAN_PHAM.TEN_SAN_PHAM;
            txtMaNhomSanPham.Text = v_obj_DM_SAN_PHAM.MA_NHOM_SAN_PHAM;
            txtGhiChu.Text = v_obj_DM_SAN_PHAM.GHI_CHU;
        }
    }

    private void BindLabel()
    {
        m_lbl_thong_bao.Text = "Không có thông báo gì.";
        this.lblF221.Text = "F110 - Thêm mới sản phẩm";
    }

    protected void gvDsMonHoc_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        int id = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName.ToUpper())
        {
            case "EDIT":
                Response.Redirect(String.Format("F110.aspx?Id={0}", id));
                break;
            case "DEL":
                bool isok = Services.DeleteDM_SAN_PHAM(id);
                if (isok)
                {
                    BindData();
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, xóa không thành công!";
                    m_lbl_mess.Visible = true;
                }
                break;
        }
    }

    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            DM_SAN_PHAM v_obj_DM_SAN_PHAM = new DM_SAN_PHAM();
            v_obj_DM_SAN_PHAM.MA_SAN_PHAM = txtMaSanPham.Text;
            v_obj_DM_SAN_PHAM.TEN_SAN_PHAM= txtTenSanPham.Text;
            v_obj_DM_SAN_PHAM.MA_NHOM_SAN_PHAM = txtMaNhomSanPham.Text;
            v_obj_DM_SAN_PHAM.GHI_CHU = txtGhiChu.Text;
            bool isOk = Services.InsertDM_SAN_PHAM(v_obj_DM_SAN_PHAM);
            if (isOk)
            {
                this.m_lbl_mess.Text = "Thêm mới thành công!";
                m_lbl_mess.Visible = true;
                BindData();
            }
            else
            {
                this.m_lbl_mess.Text = "Lỗi, thêm mới không thành công!";
                m_lbl_mess.Visible = true;

            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
        }

    }
    private void ClearForm()
    {
        txtMaSanPham.Text = "";
        txtTenSanPham.Text = "";
        txtMaNhomSanPham.Text = "";
        txtGhiChu.Text = "";
    }

    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        string v_str_ID = Request.QueryString["Id"];
        int v_int_ID = 0;
        int.TryParse(v_str_ID, out v_int_ID);
        if (v_int_ID != 0)
        {
            try
            {
                DM_SAN_PHAM v_obj_DM_SAN_PHAM = new DM_SAN_PHAM();
                v_obj_DM_SAN_PHAM.ID = v_int_ID;
                v_obj_DM_SAN_PHAM.MA_SAN_PHAM = txtMaSanPham.Text;
                v_obj_DM_SAN_PHAM.TEN_SAN_PHAM = txtTenSanPham.Text;
                v_obj_DM_SAN_PHAM.MA_NHOM_SAN_PHAM = txtMaNhomSanPham.Text;
                v_obj_DM_SAN_PHAM.GHI_CHU = txtGhiChu.Text;
                bool isOk = Services.UpdateDM_SAN_PHAM(v_obj_DM_SAN_PHAM);

                if (isOk)
                {
                    this.m_lbl_mess.Text = "Cập nhật thành công!";
                    m_lbl_mess.Visible = true;
                    BindData();
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, cập nhật không thành công!";
                    m_lbl_mess.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
            }
        }
        else
        {
            this.m_lbl_mess.Text = "Bạn hãy chọn version cần chỉnh sửa.";
        }
    }
    protected void gvDsMonHoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDsMonHoc.PageIndex = e.NewPageIndex;
        BindData();
    }
}